package com.sunnyatwal.kettlebell.clock;

import android.os.Handler;
import android.util.Log;

public abstract class MyCountDownTimer extends Thread {
	
    private static final String TAG = "MyCountDownTimer";
	
    private int mMillisInFuture;
    private int mCountDownInterval;
    
    private boolean mIsPaused;
    private boolean mIsFinished;
    private boolean mStatusOk;
    
    final Handler mHandler = new Handler();

    
    public MyCountDownTimer(int pMillisInFuture, int pCountDownInterval) {
    	
    	mMillisInFuture = pMillisInFuture;
    	mCountDownInterval = pCountDownInterval;
    	
    	mIsPaused = false;
    	mIsFinished = false;
    	mStatusOk = true;
    }

    
    public void run() {
    	
    	if(mStatusOk) {
	    	
    		if(mMillisInFuture <= 0) {
	    		mIsFinished = true;
	    		onFinish();
		    } 
	    	else {
	    		if(!mIsPaused) {	    	  
	    			mMillisInFuture -= mCountDownInterval;
	    			onTick(mMillisInFuture, mCountDownInterval);
	    		}
	    		
	    		mHandler.postDelayed(this, mCountDownInterval);
	        	Log.i(TAG, ""+mMillisInFuture);
	    	}	    	
    	}
    }
       
    
    public void togglePause() {
    	mIsPaused = !mIsPaused;
    }
    
    
    public boolean isPaused() {
    	return mIsPaused;
    }
    
    
    public boolean isFinished() {
    	return mIsFinished;
    }
    
    
    public void killTimer() {
    	mStatusOk = false;
    }
    
    
    public void extendTimer(int milliExtend) {
    	mMillisInFuture += milliExtend;
    }
    
    
    public abstract void onTick(int pMillisInFuture, int pCountDownInterval);
    
    public abstract void onFinish();
}
